package cn.ybk.order.service.impl;

import cn.ybk.basic.util.CodeGenerateUtils;
import cn.ybk.order.domain.OrderPetAcquisition;
import cn.ybk.order.dto.ConfirmDto;
import cn.ybk.order.mapper.OrderPetAcquisitionMapper;
import cn.ybk.order.service.IOrderPetAcquisitionService;
import cn.ybk.basic.service.impl.BaseServiceImpl;
import cn.ybk.pet.domain.Pet;
import cn.ybk.pet.domain.PetDetail;
import cn.ybk.pet.domain.SearchMasterMsg;
import cn.ybk.pet.mapper.PetDetailMapper;
import cn.ybk.pet.mapper.PetMapper;
import cn.ybk.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapperl;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper detailMapper;

    // 确定收购订单
    @Override
    public void confirm(ConfirmDto dto) {
        // order表 price 金额 payType 支付类型 paySn 订单支付编号 state = 1 已经支付
        OrderPetAcquisition order = orderPetAcquisitionMapperl.findById(dto.getId());
        order.setPrice(dto.getMoney());
        order.setPayType(dto.getPayType());
        order.setPaySn(CodeGenerateUtils.generateUnionPaySn());

        order.setState(1);
        orderPetAcquisitionMapperl.update(order);

        // 修改msg 修改状态 为 state = 4 订单成功
        SearchMasterMsg msg = new SearchMasterMsg();
        msg.setId(dto.getId());
        msg.setState(4);
        System.out.println(msg);
        searchMasterMsgMapper.update(msg);
        // 修改pet的价格
        Pet pet = new Pet();
        pet.setCostprice(dto.getMoney());
        pet.setId(order.getPetId());
        petMapper.update(pet);
        // 添加t_pet_detail 表中的数据
        PetDetail petDetail = new PetDetail();
        petDetail.setPetId(pet.getId());
        detailMapper.add(petDetail);

    }

    // 订单取消
    @Override
    public void cancel(Long id) {
        // t_search_master_msg修改表中shopid 为空
        System.out.println(id);
        
        // t_order_pet_acquisition 中状态改为 state =  1 返回 寻主池
        // t_pet 中删除宠物


    }
}
